package cn.javase.concurrent._设计模式._同步模式之顺序控制._固定运行顺序;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 先输出2，再输出1
 * 使用可重入锁
 */
public class Test02 {
    private volatile static boolean flag = false;
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition room = lock.newCondition();
        new Thread(() -> {
            lock.lock();
            try {
                while (!flag) {
                    room.await();
                }
                System.out.println(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }).start();
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(2);
                flag = true;
                room.signal();
            } finally {
                lock.unlock();
            }
        }).start();
    }
}

